package com.all.play.mypsm.App;

import android.os.Bundle;
import android.support.annotation.IdRes;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.widget.FrameLayout;
import android.widget.RadioGroup;

import com.all.play.mypsm.R;
import com.all.play.mypsm.base.BaseFragment;
import com.all.play.mypsm.community.fragment.CommunityFragment;
import com.all.play.mypsm.home.fragment.HomeFragment;
import com.all.play.mypsm.shoppingtcart.fragment.ShoppingCartFragment;
import com.all.play.mypsm.type.fragment.TypeFragment;
import com.all.play.mypsm.user.fragment.UserFragemnt;
import com.all.play.mypsm.utils.CheckPermissionsActivity;

import java.util.ArrayList;

import butterknife.BindView;
import butterknife.ButterKnife;

public class MainActivity extends CheckPermissionsActivity {

    @BindView(R.id.frameLayout)
    FrameLayout frameLayout;
    @BindView(R.id.rg_main)
    RadioGroup rgMain;
    private ArrayList<BaseFragment> fragments;

    //记录选中的位置
    private int position = 0;
    //只是声明出来,用来缓存存储
    private Fragment tempFragemnt;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ButterKnife.bind(this);

        //初始化fragment
        initFragment();

        initListener();


    }

    /**
     * 监听radiogroup
     *
     * @author 聪明一只哈
     * @time 2018/3/31 9:54
     */
    private void initListener() {

        rgMain.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, @IdRes int checkedId) {
                switch (checkedId) {
                    case R.id.rb_home:
                        position = 0;
                        break;
                    case R.id.rb_type:
                        position = 1;
                        break;
                    case R.id.rb_community:
                        position = 2;
                        break;
                    case R.id.rb_cart:
                        position = 3;
                        break;
                    case R.id.rb_user:
                        position = 4;
                        break;

                    default:

                        break;
                }

                //根据位置选取fragment
                BaseFragment baseFragment = getFragment(position);

                /*
                * params1,上次显示的fragment,缓存中的
                * params2,当前正要显示的
                * */
                switchFragment(tempFragemnt, baseFragment);

            }
        });


        rgMain.check(R.id.rb_home);
    }

    /**
     * 替换
     *
     * @author 聪明一只哈
     * @time 2018/3/31 10:02
     */
    private void switchFragment(Fragment fromFragment, BaseFragment nextFragment) {

        if (tempFragemnt != nextFragment) {
            tempFragemnt = nextFragment;
            if (nextFragment != null) {
                FragmentTransaction transaction =
                        getSupportFragmentManager().beginTransaction();
                //判断 nextFragment 是否添加
                if (!nextFragment.isAdded()) {
                    //隐藏当前 Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.add(R.id.frameLayout, nextFragment).commit();
                } else {
                    //隐藏当前 Fragment
                    if (fromFragment != null) {
                        transaction.hide(fromFragment);
                    }
                    transaction.show(nextFragment).commit();
                }
            }
        }
    }

    /**
     * 据位置选取fragment
     *
     * @author 聪明一只哈
     * @time 2018/3/31 9:59
     */
    private BaseFragment getFragment(int position) {
        if (fragments != null && fragments.size() > 0) {
            BaseFragment baseFragment = fragments.get(position);

            return baseFragment;
        }
        return null;
    }


    /**
     * 添加进集合
     *
     * @author 聪明一只哈
     * @time 2018/3/31 9:53
     */
    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new HomeFragment());
        fragments.add(new TypeFragment());
        fragments.add(new CommunityFragment());
        fragments.add(new ShoppingCartFragment());
        fragments.add(new UserFragemnt());

    }
}
